package 数组.easy;

import java.util.Arrays;

/**
 * @author Lw中
 * @date 2020/9/4 20:23
 */
public class QiErSi {

    /**
     * 寻找数组的中心索引
     */
    public static int pivotIndex(int[] nums) {
        // 定义两个变量：sum用于存储元素值总和，leftSum用于存储左边元素值总和
        int sum = 0, leftSum = 0;
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
        }
        for (int i = 0; i < nums.length; i++) {
            if (leftSum == sum - leftSum - nums[i]) {
                return i;
            } else {
                leftSum += nums[i];
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] arr = {1, 7, 3, 6, 5, 6};
        int i = pivotIndex(arr);
        System.out.println(i);
    }

}
